package com.woniuxy.meal.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.meal.common.ConstantData;
import com.woniuxy.meal.common.RedisKey;
import com.woniuxy.meal.dao.FoodTypeMapper;

import com.woniuxy.meal.entity.FoodType;

import com.woniuxy.meal.service.FoodTypeService;
import com.woniuxy.meal.service.dto.FoodTypeDto;
import com.woniuxy.meal.service.dto.FoodTypeTreeDto;
import com.woniuxy.meal.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜品类型表 服务实现类
 * </p>
 *
 * @author zz
 * @since 2023年06月12日
 */
@Service
public class FoodTypeServiceImpl extends ServiceImpl<FoodTypeMapper, FoodType> implements FoodTypeService {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private FoodTypeMapper foodTypeMapper;





    @Override
    public List<FoodTypeTreeDto> showFoodTypeTree() {
        //如果redis中存有类别信息，就直接取
        if (redisUtil.exist(RedisKey.getFoodTypeTreeKey())){
            String str = redisUtil.getString((RedisKey.getFoodTypeTreeKey()));
            return JSONUtil.toList(str, FoodTypeTreeDto.class);
        }else {
            QueryWrapper<FoodType> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("typeState",ConstantData.FOOD_TYPE_STATE_NORMAL).eq("parentId",0);
            List<FoodType> foodTypes = foodTypeMapper.selectList(queryWrapper);
            List<FoodTypeTreeDto> types = new ArrayList<>();
            for (FoodType foodType : foodTypes) {
                FoodTypeTreeDto typeTreeDto = BeanUtil.toBean(foodType, FoodTypeTreeDto.class);
                findChildTypeTree(typeTreeDto);
                types.add(typeTreeDto);
            }
            //将类别信息存入redis中
            String jsonStr = JSONUtil.toJsonStr(types);
            redisUtil.setString(RedisKey.getFoodTypeTreeKey(),jsonStr);
            return types;
        }
    }

    @Override
    public List<FoodTypeDto> showFoodNodeType() {
        QueryWrapper<FoodType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("typeSelf",1);
        queryWrapper.eq("typeState",1);
        List<FoodType> list = list(queryWrapper);
        List<FoodTypeDto> foodTypeDtos = BeanUtil.copyToList(list, FoodTypeDto.class);
        return foodTypeDtos;
    }


    public void findChildTypeTree(FoodTypeTreeDto foodTypeTreeDto){
        QueryWrapper<FoodType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("typeState",ConstantData.FOOD_TYPE_STATE_NORMAL).eq("parentId",foodTypeTreeDto.getId());
        List<FoodType> foodTypes = foodTypeMapper.selectList(queryWrapper);
        for (FoodType foodType : foodTypes) {
            FoodTypeTreeDto treeDto = BeanUtil.toBean(foodType, FoodTypeTreeDto.class);
            foodTypeTreeDto.getList().add(treeDto);
            findChildTypeTree(treeDto);
        }
    }


    public void findNodeTypeId(List<Long> NodeList, Long typeId) throws Exception{
        QueryWrapper<FoodType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parentId", typeId);
        List<FoodType> list = foodTypeMapper.selectList(queryWrapper);
        for (FoodType foodType : list) {
            if (foodType.getTypeSelf()==ConstantData.FOOD_TYPE_NODE){
                NodeList.add(foodType.getId());
            }
            else {
                findNodeTypeId(NodeList,foodType.getId());
            }
        }

    }

    public void findAllNodeTypeId(List<Long> NodeList, Long typeId) throws Exception{
        QueryWrapper<FoodType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parentId", typeId);
        List<FoodType> list = foodTypeMapper.selectList(queryWrapper);
        for (FoodType foodType : list) {
            NodeList.add(foodType.getId());
            if (foodType.getTypeSelf()==ConstantData.FOOD_TYPE_NOT_NODE){
                findNodeTypeId(NodeList,foodType.getId());
            }
        }

    }
}
